package belf.migrate.evaluate.model;

import belf.migrate.evaluate.type.GrammarType;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

import static belf.migrate.api.job.JobCode.SUCCESS;


/**
 * 评估返回结果基本实体类
 */
@Data
public class EvaluateBaseResult {

    private Map<String, EvaluateSinkResult> sinkStatisticsMap;

    /**
     * 总条数
     */
    private int allObjectSize;

    /**
     * 错误码
     */
    private Integer code = SUCCESS;

    /**
     * 语法风险
     */
    private Map<String, Set<GrammarType>> grammarTypes;

    /**
     * 字段风险
     */
    private Map<String, Map<String, Integer>> columnRisk;

    /**
     * 函数风险
     */
    private Map<String, Map<String, Integer>> functionRisk;

    /**
     * 索引风险
     */
    private Map<String, Map<String, Integer>> indexRisk;

    /**
     * 是否支持
     */
    private Set<String> notSupportCatalogs = new HashSet<>();

    public void addAllGrammarType(Map<String, Set<GrammarType>> grammarTypesMap) {
        if (grammarTypesMap == null || grammarTypesMap.isEmpty()) {
            return;
        }
        if (this.grammarTypes == null) {
            this.grammarTypes = new HashMap<>();
        }
        grammarTypesMap.forEach((k, v) -> {
            this.grammarTypes.computeIfAbsent(k, k1 -> new HashSet<>());
            this.grammarTypes.get(k).addAll(v);
        });
    }

    public void addColumnRisk(Map<String, Map<String, Integer>> columnRiskMap) {
        if (columnRiskMap == null || columnRiskMap.isEmpty()) {
            return;
        }
        if (this.columnRisk == null) {
            this.columnRisk = new HashMap<>();
        }
        columnRiskMap.forEach((k, v) -> {
            this.columnRisk.computeIfAbsent(k, k1 -> new HashMap<>());
            this.columnRisk.get(k).putAll(v);
        });
    }

    public void addFunctionRisk(Map<String, Map<String, Integer>> functionRiskMap) {
        if (functionRiskMap == null || functionRiskMap.isEmpty()) {
            return;
        }
        if (this.functionRisk == null) {
            this.functionRisk = new HashMap<>();
        }
        functionRiskMap.forEach((k, v) -> {
            this.functionRisk.computeIfAbsent(k, k1 -> new HashMap<>());
            this.functionRisk.get(k).putAll(v);
        });
    }

    public void addIndexRisk(Map<String, Map<String, Integer>> indexRiskMap) {
        if (indexRiskMap == null || indexRiskMap.isEmpty()) {
            return;
        }
        if (this.indexRisk == null) {
            this.indexRisk = new HashMap<>();
        }
        indexRiskMap.forEach((k, v) -> {
            this.indexRisk.computeIfAbsent(k, k1 -> new HashMap<>());
            this.indexRisk.get(k).putAll(v);
        });
    }
}
